package com.chery.foxlogrecord.demo.translator;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.chery.foxlogrecord.core.annotation.LogRecordField;
import com.chery.foxlogrecord.core.enums.EditType;
import com.chery.foxlogrecord.core.pojo.FieldDiffDTO;
import com.chery.foxlogrecord.core.translator.TranslatorTemplate;
import com.chery.foxlogrecord.demo.entity.UserFamily;
import com.chery.foxlogrecord.demo.enums.RelationEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author qiangt
 * @version 1.0
 * @date 2023/12/27 15:41
 * @description: 测试翻译模板
 */
public class UserFamilyTranslatorTemplate extends TranslatorTemplate {
    @Override
    public List<FieldDiffDTO> translate(Object oldObject, Object newObject, LogRecordField logRecordField) {
        List<FieldDiffDTO> result = new ArrayList<>();
        List<UserFamily> oldUserFamilyList = Optional.ofNullable(oldObject).map(obj -> JSON.parseArray(JSON.toJSONString(obj), UserFamily.class)).orElse(new ArrayList<>());
        List<UserFamily> newUserFamilyList = Optional.ofNullable(newObject).map(obj -> JSON.parseArray(JSON.toJSONString(obj), UserFamily.class)).orElse(new ArrayList<>());
        Map<RelationEnum, UserFamily> oldUserFamilyMap = oldUserFamilyList.stream().collect(Collectors.toMap(UserFamily::getRelation, userFamily -> userFamily));
        Map<RelationEnum, UserFamily> newUserFamilyMap = newUserFamilyList.stream().collect(Collectors.toMap(UserFamily::getRelation, userFamily -> userFamily));
        for (RelationEnum relation : RelationEnum.values()) {
            UserFamily oldUserFamily = oldUserFamilyMap.get(relation);
            UserFamily newUserFamily = newUserFamilyMap.get(relation);
            if (ObjectUtil.isNull(oldUserFamily) && ObjectUtil.isNotNull(newUserFamily)) {
                // 新增家庭成员
                FieldDiffDTO fieldDiffDTO = new FieldDiffDTO();
                fieldDiffDTO.setEditType(EditType.SAVE)
                        .setName(logRecordField.value() + "-" + relation.getComment())
                        .setOldValue(null)
                        .setNewValue(JSON.toJSONString(newUserFamily))
                        .setOldValueShow(logRecordField.nullDesc())
                        .setNewValueShow(newUserFamily.getName());
                result.add(fieldDiffDTO);
            } else if (ObjectUtil.isNull(newUserFamily) && ObjectUtil.isNotNull(oldUserFamily)) {
                // 删除家庭成员
                FieldDiffDTO fieldDiffDTO = new FieldDiffDTO();
                fieldDiffDTO.setEditType(EditType.DELETE)
                        .setName(logRecordField.value() + "-" + relation.getComment())
                        .setOldValue(JSON.toJSONString(oldUserFamily))
                        .setNewValue(null)
                        .setOldValueShow(oldUserFamily.getName())
                        .setNewValueShow(logRecordField.nullDesc());
                result.add(fieldDiffDTO);
            } else if (ObjectUtil.isNotNull(newUserFamily) && ObjectUtil.isNotNull(oldUserFamily)) {
                // 比较家庭成员的修改
                if (!StrUtil.equals(newUserFamily.getName(), oldUserFamily.getName())) {
                    FieldDiffDTO fieldDiffDTO = new FieldDiffDTO();
                    fieldDiffDTO.setEditType(EditType.UPDATE)
                            .setName(logRecordField.value() + "-" + relation.getComment())
                            .setOldValue(JSON.toJSONString(oldUserFamily))
                            .setNewValue(JSON.toJSONString(newUserFamily))
                            .setOldValueShow(oldUserFamily.getName())
                            .setNewValueShow(newUserFamily.getName());
                    result.add(fieldDiffDTO);
                }
            }
        }
        return result;
    }
}
